from it.unimi.dsi.webgraph import *
from it.unimi.dsi.law.rank import *
from it.unimi.dsi.law.stat import *
from java.lang import *
import os
import jarray
from java.io import *
import os.path
import urllib
import gzip
from java.lang import *
from java.util.concurrent import *


def wgetdataset(baseurl, dataset, extensions, baselocal): 
	urls = ['%s/%s/%s.%s' % (base, dataset, dataset, extension) for extension in extensions]
	datadir = os.path.join(baselocal, dataset)
	fpaths = [os.path.join(datadir, '%s.%s' % (dataset, extension)) for extension in extensions]
	File(datadir).mkdir()
	for (url,fpath) in zip(urls, fpaths):
		urllib.urlretrieve(url, fpath)
		if fpath.endswith('.gz'):
			decompress(fpath)

def decompress(fpath):
	f = gzip.GzipFile(fpath)
	s = f.read()
	f.close()
	fpathnogz = fpath.replace('.gz', '')
	f = open(fpathnogz, 'wb')
	f.write(s)
	f.close()	

baseurl = r'http://nexus.law.dsi.unimi.it/webdata'
dataset = 'cnr-2000'
extensions = ['properties', 'graph.gz', 'offsets.gz']
baselocal = r'c:\packages\jylab-0.2\examples\webmatrix'	



ITERS = 6
basedir = datadir
relbasename = dataset
basename = os.path.join(basedir, relbasename)
print basename

graph = BVGraph.loadOffline(String(basename))


SIZE = 100
TASKS = 10
barrier = CyclicBarrier(TASKS + 1)

class PageRankTask(Runnable):
    def __init__(self, graph, alpha, iterations, barrier):
		pagerank = PageRank(graph)
		pagerank.alpha = alpha
		pagerank.init()
		self.graph = graph
		self.alpha = alpha
		self.iterations = iterations
		self.barrier = barrier
		self.logger = StringIO('')
	def run(self):
		print >> self.logger, '[nodes = %d, links = %d]' % (graph.n, graph.m)
		for i in range(self.iterations):
			pagerank.step()
			if i > 0:
				tau = KendallTau.compute(pagerank.rank,
                                 pagerank.previousRank)
			print '[iter = %d, tau = %f]' % (i + 1, tau)
        
		
pool = Executors.newCachedThreadPool()

tasks = []
futures = []
for i in range(TASKS):
    task = PageRankTask()
    tasks.append(task)
    future = pool.submit(task)
    futures.append(future)

barrier.await()

print 'Eigenvalue computation tasks completed'


        

    
        
        
        
    
